package com.itheima.web;

import com.itheima.dto.MenuDto;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.service.RoleMenuService;
import com.itheima.support.Result;
import com.itheima.vo.MenuVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author admin
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @DubboReference
    private MenuService menuService;
    @DubboReference
    private RoleMenuService roleMenuService;

    @GetMapping("/findMenuByUserName/{userName}")
    public Result<List<MenuVO>> findMenuByUserName(@PathVariable("userName") String userName) {
        return Result.build(menuService.findMenuByUserName(userName));
    }

    @GetMapping("/findAll")
    public Result<List<Menu>> findAll() {
        return Result.build( menuService.list());
    }

    @GetMapping("/findAllMenu")
    public Result<List<MenuVO>> find() {
        return Result.build(menuService.findAllMenu());
    }

    //修改菜单或添加菜单
    @PostMapping("/saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody Menu menu) {
        return Result.build(menuService.saveOrUpdate(menu));
    }

    //根据菜单id删除菜单表种的菜单
    @GetMapping("/deleteByMenuId/{menuId}")
    public Result<Boolean> deleteByMenuId(@PathVariable("menuId") Long menuId) {
        return Result.build(menuService.removeById(menuId));
    }
    //根据角色id维护中间表
    @PostMapping("/saveRoleMenuByRole")
    public Result<Boolean> saveRoleMenuByRole(@RequestBody MenuDto menuDto){
        return Result.build(roleMenuService.saveRoleMenuByRole(menuDto));
    }

}
